package com.ndood;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.Arrays;

/**
 * A.3.3 条件查询
 */
public class MongoDemo1 {

	public static void main(String[] args) {
		MongoCredential credential = MongoCredential.createCredential("root", "admin", "pay-boy-cyp".toCharArray());
		MongoClient client = new MongoClient(new ServerAddress("121.196.208.113", 27017), Arrays.asList(credential));
		MongoDatabase spitdb = client.getDatabase("spit");//打开数据库
		MongoCollection<Document> spit = spitdb.getCollection("spit");//获取集合

		BasicDBObject bson=new BasicDBObject("userid","1013");// 构建查询条件
		FindIterable<Document> documents = spit.find(bson);//查询记录获取结果集合
		for(Document document:documents){
			System.out.println("内容："+  document.getString("content"));
			System.out.println("用户ID:"+document.getString("userid"));
			System.out.println("浏览量："+document.getInteger("visits"));
		}
		client.close();//关闭连接

	}

}
